/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

#ifndef __dylab_HashGenerator_h__
#define __dylab_HashGenerator_h__

//===========================================================================================
#include <set>
#include <boost/functional/hash/hash.hpp>

//===========================================================================================
#include <dylab/utils/exceptions/EAssertionFailed.h>

//===========================================================================================
namespace dylab {

    /** 
     */    
    template < 
        typename _ValueT
    >
    class HashGenerator
    {
    private:
        ///
        size_t mHash;
    public:
        /**
        */
        HashGenerator(uint32_t seed = 0) 
            : mHash(static_cast<size_t>(seed))
                { }

        /**
        */
        uint32_t getHash() const
                { return static_cast<uint32_t>(mHash); }

        /**
        */
        HashGenerator<_ValueT> & addValue(const _ValueT & value)
                { 
                    boost::hash_combine(mHash, value);
                    return *this; 
                }
        //-----------------------------------------------------------------------------

    public:
        //
        template < 
            typename _FactorT = uint32_t
        >
        class SortedAndFactorised
        {
        private:
            ///
            std::set<_ValueT> mValueSet;
            ///
            bool mUseFactors;
            ///
            std::vector<_FactorT> mFactors;
        public:

            /**
            */
            SortedAndFactorised(bool _useFactors = true) 
                : mUseFactors(_useFactors)
                    { }

            /**
            */
            SortedAndFactorised<_ValueT> & addValue(const _ValueT & value)
                    { 
                        DYLAB_ASSERT(!mUseFactors);
                        mValueSet.insert(value);
                        return *this;
                    }

            /**
            */
            SortedAndFactorised<_ValueT> & addValue(const _ValueT & value, const _FactorT & factor)
                    { 
                        mValueSet.insert(value);
                        mFactors.push_back(factor);
                        return *this;
                    }

            /**
            */
            HashGenerator<_ValueT> generate(uint32_t seed = 0) 
                    {
                        HashGenerator<_ValueT> h(seed);
                        uint32_t i = 0;
                        for (std::set<_ValueT>::iterator it = mValueSet.begin(); it != mValueSet.end(); it++)
                        {
                            if (mUseFactors)
                                h.addValue(*it * mFactors[i++]);
                            else
                                h.addValue(*it);
                        }
                        return h;
                    }
        };        
    };
}

//===========================================================================================
#endif // __dylab_HashGenerator_h__
